package com.jhhc.StormSurgeForecast.AstronomicalTide.Util;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.jhhc.StormSurgeForecast.AstronomicalTide.Pojo.BDMS_FBC_RESULT_P;
import com.jhhc.StormSurgeForecast.AstronomicalTide.Pojo.BDMS_ST_ASTROTD_F;
import com.jhhc.StormSurgeForecast.AstronomicalTide.Pojo.ModelInfo.StationInfo;
import com.jhhc.StormSurgeForecast.Calculate.util.ModelUtil;
import com.jhhc.StormSurgeForecast.StormSurge.Pojo.*;
import com.jhhc.StormSurgeForecast.StormSurge.Pojo.FBC.BDMS_FBC_ASTROTD_A;
import com.jhhc.StormSurgeForecast.StormSurge.Pojo.FBC.BDMS_FBC_RESULT_A;
import com.jhhc.StormSurgeForecast.StormSurge.Uitl.BinaryUtil;
import com.jhhc.Weathers.datasupplement.entity.BdmsWfProductF;
import com.jhhc.Weathers.datasupplement.entity.jcdzBO.Base;
import com.jhhc.Weathers.datasupplement.util.StructureTime;
import org.springframework.stereotype.Component;

import java.io.*;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author LYQ
 * 读取文件类
 */
@Component
public class ReadUtil {

    private static final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public static List<BDMS_FBC_RESULT_A> readFors64(String path,String calSchemeId
            ,String areaName,String time,String modelId){
        File file = new File(path);
        if (file.exists()){
            try {
                Calendar cal = Calendar.getInstance();
                cal.setTime(format.parse(time));
                if (areaName.contains("广东")) {
                    cal.add(Calendar.HOUR,1);
                }
                BufferedReader br = new BufferedReader( new FileReader(path));
                String s;
                int count = 0;
                for (int i = 0; i < 2; i++) {
                    if (i == 1) {
                        s = br.readLine();
                        String[] strings = s.split("\\s+");
                        count = Integer.parseInt(strings[2].trim());
                    }else {
                        System.out.println(br.readLine());
                    }
                }
                List<BDMS_FBC_RESULT_A>  aList = new ArrayList<>();
                StringBuilder spots1 = new StringBuilder();
                StringBuilder spots2 = new StringBuilder();
                int dtid = 1;
                while ((s = br.readLine()) != null){
                    for (int i = 0; i < count; i++) {
                        s = br.readLine();
                        if (s != null && !"".equals(s)) {
                            String[] strings  = s.split("\\s+");
                            spots1.append(ReadUtil.dataForm(strings[2].trim())).append(",");
                            spots2.append(ReadUtil.dataForm(strings[3].trim())).append(",");
                        }
                    }
                    for (int i = 0; i < 2; i++) {
                        BDMS_FBC_RESULT_A a = new BDMS_FBC_RESULT_A();
                        a.setCschemeid(calSchemeId);
                        a.setAreaname(areaName);
                        a.setDtid(dtid);
                        a.setDttm(format.format(cal.getTime()));
                        if (i == 0) {
                            a.setDttp(ModelUtil.modelType(modelId));
                            a.setDtvalues(spots1.toString());
                        }else {
                            a.setDttp(ModelUtil.modelType(modelId));
                            a.setDtvalues(spots2.toString());
                        }
                        aList.add(a);
                    }
                    spots1 = new StringBuilder();
                    spots2 = new StringBuilder();
                    dtid++;
                    cal.add(Calendar.HOUR,1);
                }
                br.close();
                return aList;
            } catch (ParseException e) {
                e.printStackTrace();
                System.out.println("时间转换异常");
                return null;
            } catch (IOException e){
                e.printStackTrace();
                System.out.println("读取文件异常");
                return null;
            }
        }else {
            System.out.println("文件不存在");
            return null;
        }
    }

    /**
     *读fort.63入BDMS_FBC_RESULT_A表方法
     */
    public static List<BDMS_FBC_RESULT_A> readFors63(String path,String calSchemeId, String areaName,
                             String time,String modelId) throws ParseException, IOException {
        File file = new File(path);
        if(file.exists()){
            Calendar cal = Calendar.getInstance();
            cal.setTime(format.parse(time));
            BufferedReader br = new BufferedReader( new FileReader(file));
            String s;
            int count = 0;
            for (int i = 0; i < 2; i++) {
                if (i == 1) {
                    s = br.readLine();
                    String[] strings = s.split("\\s+");
                    count = Integer.parseInt(strings[2].trim());
                }else {
                    br.readLine();
                }
            }
            List<BDMS_FBC_RESULT_A>  aList = new ArrayList<>();
            BDMS_FBC_RESULT_A a = null;
            StringBuilder spots = new StringBuilder();
            int dtid = 1;
            while ((s = br.readLine()) != null){
                // System.out.println(s);
                for (int i = 0; i < count; i++) {
                    if (!"".equals(s)) {
                        String[] strings = br.readLine().split("\\s+");
                        spots.append(ReadUtil.dataForm(strings[strings.length-1].trim())).append(",");
                    }
                }
                a = new BDMS_FBC_RESULT_A();
                a.setCschemeid(calSchemeId);
                a.setAreaname(areaName);
                a.setDtid(dtid);
                a.setDttm(format.format(cal.getTime()));
                a.setDttp(ModelUtil.modelType(modelId));
                a.setDtvalues(spots.toString());
                aList.add(a);
                spots = new StringBuilder();
                dtid++;
                cal.add(Calendar.HOUR,1);
            }
            br.close();
            return aList;
        }else {
            System.out.println("文件不存在");
            return null;
        }
    }
    /**
     *读fort.64入BDMS_FBC_ASTROTD_A表方法
     */
    public static List<BDMS_FBC_ASTROTD_A> read64(String path,String calSchemeId,String time){
        File file = new File(path);
        if (file.exists()) {
            try {
                Calendar cal = Calendar.getInstance();
                cal.setTime(format.parse(time));
                BufferedReader br = new BufferedReader(new FileReader(file));
                String s;
                int count = 0;
                for (int i = 0; i < 2; i++) {
                    if (i == 1) {
                        s = br.readLine();
                        String[] strings = s.split("\\s+");
                        count = Integer.parseInt(strings[2].trim());
                    }else {
                        System.out.println(br.readLine());
                    }
                }
                List<BDMS_FBC_ASTROTD_A>  aList = new ArrayList<>();
                StringBuilder spots1 = new StringBuilder();
                StringBuilder spots2 = new StringBuilder();
                while ((s = br.readLine()) != null){
                    // System.out.println(s);
                    for (int i = 0; i < count; i++) {
                        s = br.readLine();
                        //System.out.println(s);
                        String[] strings = s.split("\\s+");
                        spots1.append(ReadUtil.dataForm(strings[2].trim())).append(",");
                        spots2.append(ReadUtil.dataForm(strings[3].trim())).append(",");
                    }
                    for (int i = 0; i < 2; i++) {
                        BDMS_FBC_ASTROTD_A bdmsFbcAstrotdA = new BDMS_FBC_ASTROTD_A();
                        bdmsFbcAstrotdA.setPid(calSchemeId);
                        bdmsFbcAstrotdA.setTm(format.format(cal.getTime()));
                        if (i == 0) {
                            bdmsFbcAstrotdA.setDttp(2);
                            bdmsFbcAstrotdA.setDtvalues(spots1.toString());
                        }else {
                            bdmsFbcAstrotdA.setDttp(3);
                            bdmsFbcAstrotdA.setDtvalues(spots2.toString());
                        }
                        aList.add(bdmsFbcAstrotdA);
                    }
                    spots1 = new StringBuilder();
                    spots2 = new StringBuilder();
                    cal.add(Calendar.HOUR,1);
                }
                br.close();
                return aList;
            } catch (IOException e) {
                e.printStackTrace();
                System.out.println("文件读取异常");
                return null;
            }catch (ParseException e) {
                e.printStackTrace();
                System.out.println("时间转换异常");
                return null;
            }
        }else {
            System.out.println("当前文件不存在，文件路径：" + path);
            return null;
        }

    }

    /**
     *读fort.63入BDMS_FBC_ASTROTD_A表方法
     */
    public static List<BDMS_FBC_ASTROTD_A> read63(String path,String pid, String time){
        File file = new File(path);
        if (file.exists()) {
            try {
                Calendar cal = Calendar.getInstance();
                cal.setTime(format.parse(time));
                BufferedReader br = new BufferedReader( new FileReader(file));
                String s;
                int count = 0;
                for (int i = 0; i < 2; i++) {
                    if (i == 1) {
                        s = br.readLine();
                        String[] strings = s.split("\\s+");
                        count = Integer.parseInt(strings[2].trim());
                    }else {
                        br.readLine();
                    }
                }
                List<BDMS_FBC_ASTROTD_A> aList = new ArrayList<>();
                BDMS_FBC_ASTROTD_A bdmsFbcAstrotdA = null;
                StringBuilder spots = new StringBuilder();
                int dtid = 1;
                while ((s = br.readLine()) != null){
                    // System.out.println(s);
                    for (int i = 0; i < count; i++) {
                        String[] strings = br.readLine().split("\\s+");
                        spots.append(ReadUtil.dataForma(strings[strings.length-1].trim()));
                    }
                    bdmsFbcAstrotdA = new BDMS_FBC_ASTROTD_A();
                    bdmsFbcAstrotdA.setPid(pid);
                    bdmsFbcAstrotdA.setTm(format.format(cal.getTime()));;
                    bdmsFbcAstrotdA.setDttp(1);
                    bdmsFbcAstrotdA.setDtvalues(spots.toString());

                    aList.add(bdmsFbcAstrotdA);
                    spots = new StringBuilder();
                    dtid++;
                    cal.add(Calendar.HOUR,1);
                }
                br.close();
                return aList;
            } catch (IOException e ) {
                e.printStackTrace();
                System.out.println("读取文件异常");
                return null;
            }catch (ParseException e ){
                e.printStackTrace();
                System.out.println("时间转换异常");
                return null;
            }
        }else {
            System.out.println("当前文件不存在，文件路径：" + path);
            return null;
        }
    }

    /**
     *读fort.61入bdms_st_astrotd_f表方法
     */
    public static List<BDMS_ST_ASTROTD_F> read61(String path,String time) {
        List<StationInfo> stationInfos = ModelFileRead.getStationInfos(path);
        File file = new File(path + File.separator + "fort.61");
        if (file.exists()) {
            try {
                Calendar cal = Calendar.getInstance();
                cal.setTime(format.parse(time));
                BufferedReader br = new BufferedReader( new FileReader(file));
                String s;
                int count = 0;
                for (int i = 0; i < 2; i++) {
                    if (i == 1) {
                        s = br.readLine();
                        String[] strings = s.split("\\s+");
                        count = Integer.parseInt(strings[2].trim());
                    }else {
                        br.readLine();
                    }
                }
                List<BDMS_ST_ASTROTD_F> aList = new ArrayList<>();
                BDMS_ST_ASTROTD_F astrotd = null;
                while ((s = br.readLine()) != null){
                    for (StationInfo stationInfo : stationInfos) {
                        if ("温州".equals(stationInfo.getCity())) {
                            astrotd = new BDMS_ST_ASTROTD_F();
                            String[] strings = br.readLine().split("\\s+");
                            astrotd.setStcd(stationInfo.getStationCode());
                            astrotd.setYmdh(format.format(cal.getTime()));
                            BigDecimal bd = new BigDecimal(strings[strings.length - 1].trim());
                            String bdstr = bd.toPlainString();
                            String data = String.format("%.2f",Double.parseDouble(bdstr));
                            astrotd.setFtdz(Double.parseDouble(data));
                            astrotd.setHltdmk(0);
                            astrotd.setUnitname(0);
                            aList.add(astrotd);
                        }else {
                            br.readLine();
                        }
                    }
                    cal.add(Calendar.HOUR,1);
                }
                br.close();
                return aList;
            } catch (IOException e ) {
                e.printStackTrace();
                System.out.println("读取文件异常");
                return null;
            }catch (ParseException e ){
                e.printStackTrace();
                System.out.println("时间转换异常");
                return null;
            }
        }else {
            System.out.println("当前文件不存在，文件路径：" + path);
            return null;
        }
    }


    /**
     * 读取BTFUP文件
     * @param localPath 本地路径
     * @param calSchemeId 方案id
     * @param time 时间
     * @param areaName 区域
     * @return
     */
    public static List<BDMS_FBC_RESULT_A> readBtfup(String localPath,String calSchemeId,
                                                    String time,String areaName,int dttp) throws ParseException, IOException {
        List<String> paths = BinaryUtil.getConditions(localPath);
        List<BDMS_FBC_RESULT_A> aList = new ArrayList<>();
        BDMS_FBC_RESULT_A r;
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar cal = Calendar.getInstance();
        cal.setTime(format.parse(time));
        for (int i = 0; i < paths.size(); i++) {
            r = new BDMS_FBC_RESULT_A();
            r.setCschemeid(calSchemeId);
            r.setAreaname(areaName);
            r.setDtid(i);
            String tm = format.format(cal.getTime());
            r.setDttm(tm);
            cal.add(Calendar.HOUR,1);
            r.setDttp(dttp);
            BufferedReader br = new BufferedReader(new FileReader(paths.get(i)));
            String s = null;
            StringBuilder spots = new StringBuilder();
            while((s = br.readLine())!=null){//使用readLine方法，一次读一行
                if (!"".equals(s)) {
                    String[] data = s.split(",");
                    if (!"-99.900000".equals(data[2].trim())) {
                        spots.append(ReadUtil.dataForm(data[2].trim())).append(",");
                    }
//                    if ("NaN".equals(dataz)) {
//                        dataz = "0.0000000E+00";
//                    }
//                    BigDecimal db = new BigDecimal(dataz);
//                    String z = db.toPlainString();
//                    if (!"-99.900000".equals(z)) {
//                        double d = Double.parseDouble(z);
//                        String value = a.format(d);
//                        double dbFormat = Double.parseDouble(value);
//                        double dbs = dbFormat * 100;
//                        double d1 = Math.round(dbs);
//                        String values = "";
//                        if (d1 > 0) {
//                            values = a2.format(d1);
//                        }if (d1 < 0){
//                            values = a1.format(d1);
//                        }
//                        if (d1 == 0) {
//                            d1 = 0 - d1;
//                            values = a2.format(d1);
//                        }
//                        resilt.append(values).append(",");
//                        spots.append(resilt);
//                    }
                }
            }
            r.setDtvalues(spots.toString());
            aList.add(r);
            br.close();
        }
        return aList;
    }


    /**
     * 解析构造入库数据
     * @param path
     * @param cschemeId
     * @return
     * @throws IOException
     * @throws ParseException
     */
    public static List<BDMS_FBC_RESULT_P> readFors61(String path, String cschemeId,
                                                     String[] sations,int dttp) {
        path = path + File.separator + cschemeId + File.separator + "fort.61";
        try {
            BufferedReader br = new BufferedReader(new FileReader(path));
            String s;
            int count = 0;
            for (int i = 0; i < 2; i++) {
                if (i == 1) {
                    s = br.readLine();
                    String[] strings = s.split("\\s+");
                    count = Integer.parseInt(strings[2].trim());
                }else {
                    System.out.println(br.readLine());
                }
            }
            List<BDMS_FBC_RESULT_P>  bdms_fbc_result_pList = new ArrayList<>();
            BDMS_FBC_RESULT_P bdms_fbc_result_p = null;
            for (int i = 0; i < count; i++) {
                bdms_fbc_result_p = new BDMS_FBC_RESULT_P();
                bdms_fbc_result_p.setCschemeid(cschemeId);
                bdms_fbc_result_p.setDttp(dttp);
                bdms_fbc_result_p.setPtkey(sations[i]);
                bdms_fbc_result_p.setDtvalues("");
                bdms_fbc_result_pList.add(bdms_fbc_result_p);
            }
            while ((s = br.readLine()) != null){
                for (int i = 0; i < count; i++) {
                    s = br.readLine();
                    String[] strings = s.split("\\s+");
                    bdms_fbc_result_pList.get(i).setDtvalues(bdms_fbc_result_pList.
                            get(i).getDtvalues() + ReadUtil.dataForm(strings[strings.length - 1].trim()));
                }
            }
            br.close();
            return bdms_fbc_result_pList;
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("读取文件异常");
            return  null;
        }
    }

    /**
     * 站码数组为空，获取全部站码数据
     * @param path
     * @param forecasttime
     * @param sations
     * @return
     * @throws IOException
     * @throws ParseException
     */
    public static List<CalResult> readFors61(String path,String forecasttime,
                                             String[] sations,String calSchemeId) throws IOException, ParseException {
        path = path + File.separator + calSchemeId + File.separator + "fort.61";
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //String time = ReadUtil.yesterDay(forecasttime);
        Date date = format.parse(forecasttime);
        Date d = format.parse(forecasttime);
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        BufferedReader br = new BufferedReader( new FileReader(path));
        String s;
        int count = 0;
        int tms = 0;
        for (int i = 0; i < 2; i++) {
            if (i == 1) {
                s = br.readLine();
                String[] strings = s.split("\\s+");
                count = Integer.parseInt(strings[2].trim());
                tms = Integer.parseInt(strings[1].trim());
            }else {
                System.out.println(br.readLine());
            }
        }
        List<CalResult> resultList = new ArrayList<>();
        CalResult result = null;
        List<PointResult> pointResults = new ArrayList<>();;
        PointResult pointResult = null;
        for (int i = 0; i < count; i++) {
            result = new CalResult();
            result.setStationCode(sations[i]);
            result.setForecastTime(format.format(d));
            pointResults = new ArrayList<>();
            for (int j = 0; j < tms; j++) {
                pointResult = new PointResult();
                pointResults.add(pointResult);
            }
            result.setPointResults(pointResults);
            resultList.add(result);
        }
        int t = 0;
        while ((s = br.readLine()) != null){
            for (int i = 0; i < count; i++) {
                s = br.readLine();
                if (s != null && !"".equals(s)) {
                    String[] strings = s.split("\\s+");
                    BigDecimal bd = new BigDecimal(strings[strings.length - 1].trim());
                    String bdstr = bd.toPlainString();
                    String data = String.format("%.2f",Double.parseDouble(bdstr));
                    resultList.get(i).getPointResults().get(t).setTm(format.format(cal.getTime()));
                    resultList.get(i).getPointResults().get(t).setZs(Double.parseDouble(data));
                }
            }
            t++;
            cal.add(Calendar.HOUR,1);
        }
        br.close();
        return resultList;
    }

    /**
     * 站码数组不为空，获取对应站码的数据
     * @param path 文件路径
     * @param forecasttime  作业预报时间
     * @param stationCodes  传入站码集合
     * @param sations  全部站码集合
     * @return
     * @throws IOException
     * @throws ParseException
     */
    public static List<CalResult> readFors61(String path, String forecasttime, List<String> stationCodes,
                                             String[] sations,String calSchemeId) {
        //path = path + File.separator + calSchemeId + File.separator + "fort.61";
        //path = "C:\\Users\\LYQ\\Desktop\\数据\\fort.61";
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //String time = ReadUtil.yesterDay(forecasttime);
        Date date = null;
        try {
            date = format.parse(forecasttime);
            Date d = format.parse(forecasttime);
            Calendar cal = Calendar.getInstance();
            cal.setTime(date);
            BufferedReader br = new BufferedReader( new FileReader(path));
            String s;

            int count = 0;
            int tms = 0;
            for (int i = 0; i < 2; i++) {
                if (i == 1) {
                    s = br.readLine();
                    String[] strings = s.split("\\s+");
                    count = Integer.parseInt(strings[2].trim());
                    tms = Integer.parseInt(strings[1].trim());
                }else {
                    System.out.println(br.readLine());
                }
            }
            List<CalResult> resultList = new ArrayList<>();
            CalResult result = null;
            List<PointResult> pointResults = new ArrayList<>();;
            PointResult pointResult = null;
            for (int i = 0; i < stationCodes.size(); i++) {
                result = new CalResult();
                result.setStationCode(String.valueOf(stationCodes.get(i)));
                result.setForecastTime(format.format(d));
                pointResults = new ArrayList<>();
                for (int j = 0; j < tms; j++) {
                    pointResult = new PointResult();
                    pointResults.add(pointResult);
                }
                result.setPointResults(pointResults);
                resultList.add(result);
            }
            int t = 0;
            while ((s = br.readLine()) != null){
                for (int i = 0; i < count; i++) {
                    s = br.readLine();
                    if (s != null && !"".equals(s)) {
                        for (CalResult calResult : resultList) {
                            if (sations[i].equals(calResult.getStationCode())) {
                                String[] strings = s.split("\\s+");
                                BigDecimal bd = new BigDecimal(strings[strings.length - 1].trim());
                                String bdstr = bd.toPlainString();
                                String data = String.format("%.2f", Double.parseDouble(bdstr));
                                calResult.getPointResults().get(t).setTm(format.format(cal.getTime()));
                                calResult.getPointResults().get(t).setZs(Double.parseDouble(data));
                            }
                        }
                    }
                }
                t++;
                cal.add(Calendar.HOUR,1);
            }
            br.close();
            return resultList;
        } catch (ParseException | IOException e) {
            e.printStackTrace();
            System.out.println("文件读取异常");
            return null;
        }
    }


    public static String readnhup1(String path) {
        File file = new File(path);
        if (file.exists()) {
            try {
                BufferedReader br = new BufferedReader( new FileReader(file));
                String time = "";
                for (int i = 0; i < 3; i++) {
                    String s = br.readLine();
                    if (i == 2) {
                        String[] strins = s.split(",");
                        if (strins[1].length() < 2) {
                            strins[1] = "0" + strins[1];
                        }
                        time = strins[0] + "-" + strins[1] + "-0" + strins[2] + " 0" + (Integer.parseInt(strins[3])) + ":00:00";
                    }
                }
                br.close();
                return time;
            } catch (IOException e) {
                e.printStackTrace();
                System.out.println("文件读取异常");
                return  null;
            }
        }else {
            System.out.println("文件不存在");
            return  null;
        }
    }

    public static String dataForm(String str){
        if ("NaN".equals(str) || str == null || "".equals(str)) {
            return "000000";
        }else {
            DecimalFormat a1 = new DecimalFormat("000000");
            DecimalFormat a2 = new DecimalFormat("#00000");
            BigDecimal bd = new BigDecimal(str);
            String bdstr = bd.toPlainString();
            String data = String.format("%.2f",Double.parseDouble(bdstr));
            if ("-99999.00".equals(data)) {
                return "-99000";
            }
            int count = (int) (Double.parseDouble(data) * 100);
            //System.out.println(count);
            if (count >= 0) {
                return a1.format(count);
            }else {
                return a2.format(count);
            }
        }
    }
    public static String dataForma(String str){
        DecimalFormat a1 = new DecimalFormat("00.0000000");
        DecimalFormat a2 = new DecimalFormat("#00.000000");
        BigDecimal bd = new BigDecimal(str);
        String bdstr = bd.toPlainString();
        String data = String.format("%.2f",Double.parseDouble(bdstr));
        String s = "";
        if ("-99999.00".equals(data)) {
            s = "-990000000";
            return s;
        }
        double count = Double.parseDouble(data);
        if (count >= 0) {
            s = a1.format(Math.abs(count));
        }else {
            s = a2.format(count);
        }
        return  s;
    }

    public static String yesterDy(String time) throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = format.parse(time);
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DATE,-1);
        return format.format(cal.getTime());
    }

    /**
     *
     * @param path 路径
     * @param calSchemeId 计算方案id
     * @param stationCodes 传入的站码集合
     * @return
     */
    public static List<CalResult> readtfupresuls(String path,String calSchemeId,
                                                 List<String> stationCodes){
        path = path + File.separator + calSchemeId + File.separator + "tfupresuls.dat";
        String yearPath = path + File.separator + calSchemeId + File.separator + "nhup1.txt";
        File file = new File(path);
        if (file.exists()) {
            List<String> strings = new ArrayList<>();
            List<String> stationCode = new ArrayList<>();
            String value;
            FileInputStream fis = null;
            InputStreamReader isr = null;
            BufferedReader br = null;
            try {
                fis = new FileInputStream(file);
                isr = new InputStreamReader(fis,"GB2312");
                br = new BufferedReader(isr);
                while ((value = br.readLine()) != null){
                    strings.add(value);
                }
                fis.close();
                isr.close();
                br.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            //获取站码集合
            DecimalFormat format = new DecimalFormat("0000");
            String[] sort = strings.get(6).split("\\s+");
            String[] nm1 = strings.get(7).split(",");
            String q,h;
            for (int i = 0; i < 39; i++) {
                q = format.format(Integer.parseInt(sort[i+2].trim()));
                h = format.format(Integer.parseInt(nm1[i].trim()));
                stationCode.add(q + h);
            }
            String year = ReadUtil.startAndEndTime(yearPath);
            List<CalResult> calResults = ReadUtil.returnObject(strings.size(),stationCode);
            int p = 0;
            for (int i = 8; i < strings.size(); i++) {
                String[] values = strings.get(i).split(",");
                String time = ReadUtil.setTime(year,values[1].trim(),values[2].trim(),values[3].split("\\.")[0].trim());
                for (int j = 4; j < values.length - 1; j++) {
                    calResults.get(j - 4).getPointResults().get(p).setTm(time);
                    calResults.get(j - 4).getPointResults().get(p).setTwc(Double.parseDouble(values[j].trim()));
                }
                p++;
            }
            List<CalResult> calResults1 = new ArrayList<>();
            for (int i = 0; i < stationCodes.size(); i++) {
                int finalI = i;
                calResults1.add(calResults.stream().filter(v -> v.getStationCode().equals(stationCodes.get(finalI))).findFirst().get());
            }
            return calResults1;
        }else {
            return null;
        }


    }
    public static String startAndEndTime(String path){
        List<String> strings = new ArrayList<>();
        path = path + File.separator + "nhup1.txt";
        File file = new File(path);
        String value;
        if (file.exists()) {
            try {
                FileInputStream fis = new FileInputStream(file);
                InputStreamReader isr = new InputStreamReader(fis);
                BufferedReader br = new BufferedReader(isr);
                while ((value = br.readLine()) != null){
                    strings.add(value);
                }
                fis.close();
                isr.close();
                br.close();
                String[] startTime = strings.get(2).split(",");
                return startTime[0];
            } catch (IOException e) {
                e.printStackTrace();
                return null;
            }
        }else {
            return null;
        }
    }

    /**
     * 创建返回对象集合
     * @param count 数据集合长度
     * @param stationCode 站码集合长度
     * @return 返回对象集合
     */
    public static List<CalResult> returnObject(int count, List<String> stationCode){
        List<CalResult> calResults = new ArrayList<>();
        CalResult calResult;
        List<PointResult> pointResults;
        PointResult pointResult;
        for (String s : stationCode) {
            calResult = new CalResult();
            calResult.setStationCode(s);
            pointResults = new ArrayList<>();
            for (int j = 8; j < count; j++) {
                pointResult = new PointResult();
                pointResults.add(pointResult);
            }
            calResult.setPointResults(pointResults);
            calResults.add(calResult);
        }
        return calResults;
    }

    /**
     * 根据传入的年月日时返回时间，格式yyyy-MM-dd HH:mm:ss
     * @param year 年
     * @param month 月
     * @param day 日
     * @param hour 时
     * @return 返回设置的时间
     */
    public static String setTime(String year, String month, String day, String hour) {
        DecimalFormat format = new DecimalFormat("00");
        year = format.format(Integer.parseInt(year));
        day = format.format(Integer.parseInt(day));
        hour = format.format(Integer.parseInt(hour));
        return year + "-" + month + "-" + day + " " + hour + ":00:00";
    }

}
